package com.wlts.wzy.sys_shiro.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wlts.wzy.Result.Result;
import com.wlts.wzy.sys_shiro.entity.SysRole;
import com.wlts.wzy.sys_shiro.entity.SysUser;
import com.wlts.wzy.sys_shiro.entity.SysUserRole;
import com.wlts.wzy.sys_shiro.service.impl.SysRoleServiceImpl;
import com.wlts.wzy.sys_shiro.service.impl.SysUserRoleServiceImpl;
import com.wlts.wzy.sys_shiro.service.impl.SysUserServiceImpl;
import com.wlts.wzy.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wuzhiyong
 * @since 2019-03-27
 */
@ApiIgnore
@RestController
@RequestMapping("/sys-user")
public class SysUserController {

    @Autowired
    private SysUserServiceImpl sysUserService;
    @Autowired
    private SysUserRoleServiceImpl sysUserRoleService;
    @Autowired
    private SysRoleServiceImpl roleService;

    /**
     * 未登录，shiro应重定向到登录界面，此处返回未登录状态信息由前端控制跳转页面
     *
     * @return
     */
    @RequestMapping(value = "/unauth")
    public Result unauth() {
        return new Result<>(100000, "登录凭证失效");
    }


    //增加管理员
    @RequestMapping("addSysUser")
    public Result addSysUser(@RequestBody SysUser sysUser) {

        SysUser sysUser1 = sysUserService.getOne(new QueryWrapper<SysUser>().eq("name", sysUser.getName()).eq("del_flag", "0"));
        if (sysUser1 == null) {
            sysUser.setUpdateDate(LocalDateTime.now());
            sysUser.setCreateDate(LocalDateTime.now());
            sysUser.setSalt("manager");
            String pass = sysUser.getPassword();
            String passtheword = "";
            try {
                passtheword = MD5.md5(pass, "");
            } catch (Exception e) {
                e.printStackTrace();
            }
            sysUser.setPassword(passtheword);
            if (sysUser.getRemarks() == null) {
                sysUser.setRemarks("");
            }
            sysUserService.save(sysUser);
            sysUser1 = sysUserService.getOne(new QueryWrapper<SysUser>().eq("name", sysUser.getName()).eq("del_flag", "0"));
              return Result.buildResult(sysUser1.getId());
        } else {
            return new Result<>(500, "用户名重复");
        }

    }

    //删除管理员
    @RequestMapping("deleteSysUser")
    public Result deleteSysUser(@RequestBody SysUser sysUser) {
        sysUser = sysUserService.getById(sysUser.getId());
        if (sysUser != null) {
            sysUser.setUpdateDate(LocalDateTime.now());
            sysUser.setDelFlag("1");
            sysUserService.updateById(sysUser);
        }
          return new Result<>();

    }

    //修改密码
    @RequestMapping("changeSysUser")
    public Result changeSysUser(@RequestBody SysUser sysUser) {
        if (sysUser.getPassword() == null || sysUser.getPassword().equals("")) {
            return new Result<>(500, "请输入密码");
        }
        String pass = sysUser.getPassword();
        String passtheword = "";
        try {
            passtheword = MD5.md5(pass, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        sysUser.setPassword(passtheword);
        sysUser.setUpdateDate(LocalDateTime.now());
        sysUserService.updateById(sysUser);
          return new Result<>();

    }

    /**
     * 创建超级用户--数据库没有管理用户时候
     */
    //先查询用户表有没有数据
    @RequestMapping("selectAllCount")
    public Result selectAllCount() {
        int Count = sysUserService.count(new QueryWrapper<SysUser>());
        if (Count == 0) {
              return Result.buildResult(0);
        } else {
              return Result.buildResult(1);
        }
    }

    //增加超级管理员
    @RequestMapping("addRootUser")
    public Result addRootUser(@RequestBody SysUser sysUser) {
        int Count = sysUserService.count(new QueryWrapper<SysUser>());
        if (Count != 0) {
            return new Result<>(500, "已经存在超级管理员");
        }
        sysUser.setUpdateDate(LocalDateTime.now());
        sysUser.setSalt("root");
        sysUser.setRemarks("超级管理员");
        String pass = sysUser.getPassword();
        String passtheword = "";
        try {
            passtheword = MD5.md5(pass, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        sysUser.setPassword(passtheword);
        //创建系统用户并且赋值角色
        sysUserService.save(sysUser);
        sysUser = sysUserService.getOne(new QueryWrapper<SysUser>().eq("name", sysUser.getName()).eq("del_flag", "0"));
        //得到所有的角色
        List<SysRole> sysRoleList = roleService.list(new QueryWrapper<>());
        for (SysRole sysRole : sysRoleList
        ) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(sysUser.getId());
            sysUserRole.setRoleId(sysRole.getId());
            sysUserRoleService.save(sysUserRole);
        }
          return new Result<>();
    }


}
